

import java.sql.*;
import javax.swing.JOptionPane;

public class Room {
    final static int TYPE_SINGLE = 0;
    final static int TYPE_DOUBLE = 1;
    final static int TYPE_TRIPLE = 2;
    final static int TYPE_QUAD = 3;
    
    final static int GENDER_MALE = 0;
    final static int GENDER_FEMALE = 1;
    final static int GENDER_COED = 2;

    
    
    public int roomID;
    public int buildingID;
    public int floor;
    public int roomNumber;
    public int gender;
    public int type;
    public boolean isAvail;
    public boolean forFreshman;
    public String description;    
    
    private Connection connect;

    public Room(int buildingID, int floor, int roomNumber, int gender, int type, 
            boolean isAvail, boolean forFreshman, String description) {
        this.buildingID = buildingID;
        this.floor = floor;
        this.roomNumber = roomNumber;
        this.gender = gender;
        this.type = type;
        this.isAvail = isAvail;
        this.forFreshman = forFreshman;
        this.description = description;
    }
    
    
    
    
    public Room(int roomID, Connection connect){
        this.connect = connect;
        this.roomID = roomID;
        loadFromDB();
    }
    
    
    //Insert new entry to db
    public boolean addToDB(Connection connect){
        try {
            String addQuery = "INSERT into DORMS.room (buildingID, floor, roomNumber,"
                    + "gender,type,isAvail,forFreshman,description) VALUES (?,?,?,?,?,?,?,?)";
            PreparedStatement addStmt = connect.prepareStatement(addQuery);
            addStmt.setInt(1, buildingID);
            addStmt.setInt(2, floor);
            addStmt.setInt(3, roomNumber);
            addStmt.setInt(4, gender);
            addStmt.setInt(5, type);
            addStmt.setBoolean(6, isAvail);
            addStmt.setBoolean(7, forFreshman);
            addStmt.setString(8, description);
            addStmt.executeUpdate();
            
            
            Statement stmt = connect.createStatement();
            
            ResultSet result = stmt.executeQuery("SELECT last_insert_id() from room");
            result.next();
            roomID = result.getInt("last_insert_id()");
            return true;
            
        }catch (Exception ex){
            JOptionPane.showMessageDialog(null,
                    "Unable to add to database");
             
        }
        return false;
    }
    
    
    
    
    public boolean update(){
        try {
            String updateQuery = "UPDATE DORMS.room SET buildingID = ?, floor = ?,"
                    + "roomNumber = ?, gender = ?, type = ?, isAvail = ?, "
                    + "forFreshman = ?, description = ?  WHERE roomID = ?";
            PreparedStatement updateStmt = connect.prepareStatement(updateQuery);
            updateStmt.setInt(1, buildingID);
            updateStmt.setInt(2, floor);
            updateStmt.setInt(3, roomNumber);
            updateStmt.setInt(4, gender);
            updateStmt.setInt(5, type);
            updateStmt.setBoolean(6, isAvail);
            updateStmt.setBoolean(7, forFreshman);
            updateStmt.setString(8, description);
            updateStmt.setInt(9, roomID);
            updateStmt.executeUpdate();
            return true;
        }catch (Exception ex){
            JOptionPane.showMessageDialog(null,
                    "Unable to update to database");
             
        }
        return false;
    }
    
    
    public boolean delete(){
        try {
            String deleteQuery = "DELETE FROM DORMS.room WHERE roomID = ?";
            PreparedStatement updateStmt = connect.prepareStatement(deleteQuery);
            updateStmt.setInt(1, roomID);
            updateStmt.execute();
            return true;
        }catch (Exception ex){
            JOptionPane.showMessageDialog(null,
                    "Unable to delete from database");
             
        }
        return false;
        
        
    }
    
    public String toString(){
        String buildingName = "";
        
        String query = "SELECT buildingName from DORMS.building WHERE buildingID = " + buildingID;
        try{
            Statement stmt = connect.createStatement();
            ResultSet result = stmt.executeQuery(query);
            if(result.next()){
              buildingName = result.getString(1);  
            }
            
            
        }catch(Exception e){
            JOptionPane.showMessageDialog(null,
                    "Unable to from building name");
             
        }
        
        
        return buildingName + " " + String.valueOf(roomNumber);
    }
     
    
    
    
    //Load object with info from db
    private void loadFromDB(){
        try {
            String loadQuery = "select * from room where roomID = ?";
            PreparedStatement loadStmt = connect.prepareStatement(loadQuery);
            loadStmt.setInt(1, roomID);
            ResultSet roomInfo = loadStmt.executeQuery();
            if(roomInfo.next()){
                buildingID = roomInfo.getInt("buildingID");
                floor = roomInfo.getInt("floor");
                roomNumber = roomInfo.getInt("roomNumber");
                gender = roomInfo.getInt("gender");
                type = roomInfo.getInt("type");
                isAvail = roomInfo.getBoolean("isAvail");
                forFreshman = roomInfo.getBoolean("forFreshman");
                description = roomInfo.getString("description");
            }
            
            
            
        } catch (Exception ex){
            JOptionPane.showMessageDialog(null,
                    "Unable to load room from database.");
            
        }
        
    }
    
    
}
